﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverApp.ViewModel
{
    public class RegisterViewModel : ViewModelBase
    {
        private LiveService.User            newUser;
        private String                      email2;
        private String                      passw2;
        private String                      primaryComputer;
        private ICommand                    registerCommand;
        private MainPageViewModel           mparent;

        public RegisterViewModel(MainPageViewModel parent)
        {
            mparent = parent;
            this.NewUser = new LiveService.User();
            registerCommand = new RelayCommand(RegisterUser, CanRegisterUser);
        }

        public LiveService.User NewUser
        {
            get { return this.newUser; }
            set { this.newUser = value; }
        }

        public String EmailConfirm
        {
            get { return email2; }
            set { this.email2 = value; }
        }

        public String PasswordConfirm
        {
            get { return passw2; }
            set { this.passw2 = value; }
        }

        public String Firstname
        {
            get { return this.NewUser.firstname; }
            set { this.NewUser.firstname = value; }
        }

        public String Lastname
        {
            get { return this.NewUser.lastname; }
            set { this.NewUser.lastname = value; }
        }

        public String Email
        {
            get { return this.NewUser.email; }
            set { this.NewUser.email = value; }
        }

        public String Login
        {
            get { return this.NewUser.login; }
            set { this.NewUser.login = value; }
        }

        public String Password
        {
            get { return this.NewUser.password; }
            set { this.NewUser.password = value; }
        }

        public String PrimaryComputer
        {
            get { return this.primaryComputer; }
            set { this.primaryComputer = value; OnPropertyChanged("PrimaryComputer"); }
        }

        public ICommand RegisterCommand
        {
            get { return this.registerCommand; }
            set { this.registerCommand = value; }
        }

        public void RegisterUser(object parameter)
        {
            if (this.CheckForm() == true)
            {
                try
                {
                    this.mparent.Message = "Register started : " + this.Lastname + " et " + this.Firstname;
                    this.Sclient.registerNewUserCompleted += new EventHandler<LiveService.registerNewUserCompletedEventArgs>(RegisterUserCompleted);
                    this.Sclient.registerNewUserAsync(this.NewUser, this.primaryComputer);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        public void RegisterUserCompleted(object sender, LiveService.registerNewUserCompletedEventArgs e)
        {
            try
            {
                // A MODIFIER AVEC SI FALSE == MESSAGE ERREUR
                if (e.Result == true)
                {
                    this.mparent.Message = "Register completed";
                    this.mparent.RegWin.Close();
                    this.Alert("Register completed");
                    this.Sclient.getUserProfileByLoginCompleted += new EventHandler<LiveService.getUserProfileByLoginCompletedEventArgs>(LoadUserCompleted);
                    this.Sclient.getUserProfileByLoginAsync(this.NewUser.login);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void LoadUserCompleted(object sender, LiveService.getUserProfileByLoginCompletedEventArgs e)
        {
            try
            {
                this.NewUser = e.Result;
                this.mparent.RegisterUser(this.NewUser);
                this.mparent.RegisterNextView("Home");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public bool CheckForm()
        {
            bool result = true;

            if (this.PasswordConfirm != this.Password)
            {
                result = false;
                this.mparent.Message = "Error in Password : " + this.PasswordConfirm + " != " + this.Password;
            }
            else if (this.Email != this.EmailConfirm)
            {
                result = false;
                this.mparent.Message = "Error in email";
            }
            else
            {
                result = true;
            }
            return result;
        }

        public bool CanRegisterUser(object parameter)
        {
            return true;
        }
    }
}
